package com.shop.service.impl;

import com.shop.dao.*;
import com.shop.dao.impl.*;
import com.shop.entity.*;
import com.shop.service.IOrderService;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

public class OrderServiceImpl implements IOrderService {

    private IOrderDao orderDao = new OrderDaoImpl();
    private IItemDao itemDao = new ItemDaoImpl();
    private ICartDao cartDao = new CartDaoImpl();
    private IAddressDao addressDao = new AddressDaoImpl();
    private IProductDao productDao = new ProductDaoImpl();

    /**
     * 生成订单 1、将订单存入数据库，2、将购物车中的商品存入数据库item中，并且清空购物车
     * @param uid
     * @param aid
     * @return
     */
    /*
    private String oid;
    private int uid;
    private int aid;
    private Address address;        //表中没有
    private BigDecimal ocount;       //订单总金额
    private Date otime;
    private int ostate; //订单状态 0 未付款，1已经付款未发货 2发货待收货 3 收货待评价 4订单完成 5 退货状态
    private List<Item> items;       //表中没有
    */
    @Override
    public void create(int uid, int aid) {
        Orders orders = new Orders();

        String oid = System.currentTimeMillis()+""+uid;
        orders.setOid(oid);
        orders.setUid(uid);
        orders.setAid(aid);

        List<Cart> carts = cartDao.findCartsByUid(uid);
        BigDecimal totalPrice = new BigDecimal("0");
        for(Cart c:carts){

            Product product = productDao.findProductByPid(c.getPid());
            c.setProduct(product);

            totalPrice = totalPrice.add(c.getCcount());

        }
        cartDao.clear(uid);
        orders.setOcount(totalPrice);
        orders.setOtime(new Date());
        orders.setOstate(0);

        //将订单加入数据库
        orderDao.insert(orders);

        for(Cart c:carts){
            //把购物车中商品加入item，并清空购物车
            Item item = new Item();
            item.setOid(oid);
            item.setPid(c.getPid());

            item.setIcount(c.getCcount());
            item.setInum(c.getCnum());
            itemDao.insert(item);
            /*
            *   private int cid;
                private int uid;
                private int pid;
                private Product product;
                private int cnum = 0;  //购车商品数量
                private BigDecimal ccount; //购物车小计
            * */

            /*
                private int iid;
                private String oid;
                private int pid;
                private Product product;    //表中没有
                private BigDecimal icount;
                private int inum;
            * */

        }
    }

    /**
     * 查询所有用户订单
     * @param uid
     * @return
     */
    @Override
    public List<Orders> findAllOrderByUid(int uid) {
        List<Orders> orders = orderDao.findAllOrderByUid(uid);
        for(Orders o : orders){
            List<Item> items = itemDao.findItemByOid(o.getOid());
            for(Item i : items){
                i.setProduct(productDao.findProductByPid(i.getPid()));
            }
            o.setItems(items);
            o.setAddress(addressDao.findAddressByAid(o.getAid()));
        }
        return orders;
    }

    /**
     * 通过订单编号查找指定订单
     * @param oid
     * @return
     */
    @Override
    public Orders findAllOrderByOid(String oid) {
        Orders orders = orderDao.findAllOrderByOid(oid);
        Address address = addressDao.findAddressByAid(orders.getAid());
        orders.setAddress(address);
        List<Item> items = itemDao.findItemByOid(oid);
        for(Item i : items){
            i.setProduct(productDao.findProductByPid(i.getPid()));
        }
        orders.setItems(items);
        return orders;
    }

    /**
     * 改变订单状态
     * @param oid
     * @param status
     * @return
     */
    @Override
    public int changeStatus(String oid,int status) {
        return orderDao.changeStatus(oid,status);
    }

}
